//
//  CityViewController.m
//  NBTripe
//
//  Created by 陈曦 on 16/1/7.
//  Copyright © 2016年 Vicki. All rights reserved.
//

#import "CityViewController.h"
#import "CityTableViewCell.h"
#import "XYTagViewController.h"

@interface CityViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (strong, nonatomic) NSMutableArray *dataSource;
@property (strong, nonatomic) UITableView *tableView;
@property (nonatomic,strong) NSArray  *cityArray;

@end

@implementation CityViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"城市猎人";
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [_tableView registerClass:[CityTableViewCell class] forCellReuseIdentifier:@"CityTableViewCellIdentifier"];
    [self.view addSubview:self.tableView];
    [self getData];

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"北京" style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonItemDidPress:)];
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"筛选" style:UIBarButtonItemStyleDone target:self action:@selector(rightBarButtonItemDidPress:)];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (void)leftBarButtonItemDidPress:(UIBarButtonItem *)sender {
    
    
    
}

- (void)rightBarButtonItemDidPress:(UIBarButtonItem *)sender {
    XYTagViewController *tagVC = [[XYTagViewController alloc] init];
    [self presentViewController:tagVC animated:YES completion:^{
        
    }];
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 300;
}

-(void)getData {

    [XYNetWorkTool getWithUrl:@"http://api.breadtrip.com/hunter/products/more/?city_name=%E5%8C%97%E4%BA%AC&lat=38.88259916900391&lng=121.5394698072543&sorted_id=1&start=0&tab_list=" parameter:nil httpHeader:nil responseType:ResponseTypeJSON success:^(id result) {
        NSArray *array = [NSArray arrayWithArray:result[@"product_list"]];
        NSMutableArray *arr = [NSMutableArray arrayWithCapacity:0];
        for (NSDictionary *dic in array) {
            CityModel *cityModel = [[CityModel alloc] initWithDataSource:dic];
            [arr addObject:cityModel];
        }
        self.cityArray = [NSArray arrayWithArray:arr];
        
        [self.tableView reloadData];
    } fail:^(NSError *error) {
        NSLog(@"+++++%@", error);
    }];
}






- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 100;
}




- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CityTableViewCellIdentifier"];
    cell.cityModel = self.cityArray[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    WebViewController *web = [[WebViewController alloc] init];
    web.url = LRURL([self.cityArray[indexPath.row] product_id]);
    [self.navigationController pushViewController:web animated:YES];
}


@end